package com.zhanghe.mall.web.controller;


import com.zhanghe.mall.pojo.Account;
import com.zhanghe.mall.service.AccountService;
import com.zhanghe.mall.service.vo.AccountPermissionsVO;
import com.zhanghe.mall.web.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class AccountController {

    @Autowired
    private AccountService accountService;


    @Value("${zhanghe.system-id}")
    private Long systemId;

    /**
     * 获取当前用户信息
     * @return
     */
    @GetMapping("/auth/user")
    public R getCurrentUser(){
        String userName =  SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        Account account = accountService.findByUserName(userName);
        return new R(account);
    }

    /**
     * 获取当前用户菜单权限列表
     * @return
     */
    @GetMapping("/auth/menu")
    public R getMenu(){
        String userName = (String)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Account account = accountService.findByUserName(userName);
        List<AccountPermissionsVO> list = accountService.findAllPermissions(account.getId(),systemId);
        return new R(list);
    }
}
